<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')" align-center :close-on-click-modal="false" draggable overflow>
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="90" label-position="top">
            <el-form-item label="收款数据" prop="to_account">
                <sc-form-table ref="table" class="formtable" v-model="form.to_account" placeholder="暂无数据" hideAdd hideDelete :addTemplate="addTemplate" height="250">
                    <el-table-column label="接收账户" align="center" prop="account"></el-table-column>
                    <el-table-column label="接收金额" align="center" prop="amount"></el-table-column>
                    <el-table-column label="发送手续费" align="center" prop="send_handling_fee"></el-table-column>
                    <el-table-column label="接收手续费" align="center" prop="handling_fee">
                        <template #default="scope">
                            <el-input v-model="scope.row.handling_fee" placeholder="请输入手续费" clearable @input="calcTo(scope.row)" @focus="common.inputFocus($event)" class="inputDeep"></el-input>
                        </template>
                    </el-table-column>
                    <el-table-column label="实收金额" align="center" prop="real_amount"></el-table-column>
                    <el-table-column label="备注" align="center" prop="remark"></el-table-column>
                </sc-form-table>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			dialog: {
				client: false
			},
			mode: "add",
			titleMap: {
				add: '手续费',
                show: '手续费详情',
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: [],
                status: 2,
                to_account: [],
			},
			//验证规则
			rules: {
                status: [
					{ required: true, message: '请选择通过拒绝' }
				],
            },
            accountList: [],
		}
	},
	mounted() {
        this.getAccount();
	},
	methods: {
        // 账号
        async getAccount() {
            var res = await this.$API.basic.account.all.get();
            this.accountList = res.data
        },
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
            console.log('this.form :>> ', this.form);
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
                    var res = await this.$API.business.finance.return.update.post(this.form);
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
            this.form.id = data.id
            var res = await this.$API.business.finance.return.detail.get({id: data.id})
            let b = []
            res.data.to_account.forEach((item) => {
                b.push({
                    amount: item.amount,
                    account: `【${item.account_bank}】${/^\d+$/.test(item.account) ? item.account.slice(-4) : ''}`,
                    account_bank: item.account_bank,
                    send_handling_fee: item.send_handling_fee,
                    handling_fee: 0,
                    real_amount: Number(item.amount) - Number(item.send_handling_fee),
                    remark: item.remark
                })
            })
            this.form.to_account = b
		},
        calcTo(row){
            row.real_amount = Number(row.amount) - Number(row.send_handling_fee) - Number(row.handling_fee)
        },
	}
}
</script>

<style lang="scss">
.el-input-group__append{
    padding: 0 8px;
}
</style>
